//
//  CourseContentViewController.swift
//  BellManage
//
//  Created by Bestsch on 16/4/1.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class CourseContentViewController: BaseViewController,MWPhotoBrowserDelegate {

    var titleLabel: UILabel!
    var senderLabel: UILabel!
    var imageView: PicProgressView!
    
    var dic: JSON!
    
    var photoArray = NSMutableArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.white
        self.title = "课程表"
        
        InitViews()
        
        NotificationCenter.default.addObserver(self, selector: #selector(CourseContentViewController.showBigImage(_:)), name: NSNotification.Name(rawValue: "showBigImage"), object: nil)
    }

    func InitViews() {
        
        let sender = dic["teaname"].stringValue
        let time = dic["datatime"].stringValue
        let title = dic["classname"].stringValue
        let imageUrl = dic["imageurl"].stringValue
        
        titleLabel = UILabel.init(frame: CGRect(x: 20, y: 84, width: screenW - 40, height: 40))
        titleLabel.font = UIFont.boldSystemFont(ofSize: 16)
        titleLabel.textColor = UIColor.darkText
        titleLabel.textAlignment = NSTextAlignment.center
        titleLabel.text = title + " 课程表"
        self.view.addSubview(titleLabel)
        
        senderLabel = UILabel.init(frame: CGRect(x: 20, y: titleLabel.frame.maxY, width: screenW - 40, height: 40))
        senderLabel.font = UIFont.systemFont(ofSize: 14)
        senderLabel.textColor = textlight
        senderLabel.textAlignment = NSTextAlignment.left
        senderLabel.text = sender + "老师 发表于 " + time.replacingOccurrences(of: "T", with: " ")
        self.view.addSubview(senderLabel)
        
        imageView = PicProgressView.init(frame: CGRect(x: 0, y: senderLabel.frame.maxY, width: screenW, height: screenH - 200))
        
        imageView.imageView.contentMode = UIViewContentMode.scaleAspectFit
        imageView.isUserInteractionEnabled = true
        let showImage = UITapGestureRecognizer.init(target: self, action: #selector(CourseContentViewController.showImage))
        imageView.addGestureRecognizer(showImage)
        self.view.addSubview(imageView)
        let photo = imageUrl.replacingOccurrences(of: "../..", with: "").replacingOccurrences(of: " ", with: "")
        let imageurl = server + priImg + photo + "&width=400&mode=W"
        imageView.imageUrl = imageurl
    
    }
    /**
     显示大图
     */
    func showImage() {
        let imageUrl = dic["imageurl"].stringValue
        let index = "0"
        NotificationCenter.default.post(name: Notification.Name(rawValue: "showBigImage"), object: nil, userInfo: ["url" : imageUrl, "index" : index])
    }
    
    /**
     显示大图
     
     - parameter notification:
     */
    func showBigImage(_ notification: Notification) {
        let dic: NSDictionary = (notification as NSNotification).userInfo! as NSDictionary
        let imageUrl = dic.object(forKey: "url") as! String
        let index = UInt(dic.object(forKey: "index") as! String)
        let array = imageUrl.components(separatedBy: "|") as NSArray
        let array1 = NSMutableArray()
        array1.addObjects(from: array as! [Any])
        let n = array1.count
        photoArray = NSMutableArray()
        for i in 0...n-1 {
            var url = array1.object(at: i) as! String
            url = server + url.replacingOccurrences(of: "../..", with: "").replacingOccurrences(of: " ", with: "")
            photoArray.add(MWPhoto.init(url: URL.init(string: url)))
            
        }
        let browser: MWPhotoBrowser = MWPhotoBrowser.init(delegate: self)
        // Set options
        browser.displayActionButton = true; // Show action button to allow sharing, copying, etc (defaults to YES)
        browser.displayNavArrows = false; // Whether to display left and right nav arrows on toolbar (defaults to NO)
        browser.displaySelectionButtons = false; // Whether selection buttons are shown on each image (defaults to NO)
        browser.zoomPhotosToFill = true; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
        browser.alwaysShowControls = false; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
        browser.enableGrid = true; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
        browser.startOnGrid = false; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
        browser.autoPlayOnAppear = false; // Auto-play first video
        
        // Customise selection images to change colours if required
        //        browser.customImageSelectedIconName = @"ImageSelected.png";
        //        browser.customImageSelectedSmallIconName = @"ImageSelectedSmall.png";
        //
        //        // Optionally set the current visible photo before displaying
        browser.setCurrentPhotoIndex(index!)
        self.navigationController?.pushViewController(browser, animated: true)
        browser.showNextPhoto(animated: true)
        browser.showPreviousPhoto(animated: true)
    }
    func numberOfPhotos(in photoBrowser: MWPhotoBrowser!) -> UInt {
        return UInt(photoArray.count)
    }
    func photoBrowser(_ photoBrowser: MWPhotoBrowser!, photoAt index: UInt) -> MWPhotoProtocol! {
        
        if index < UInt(photoArray.count) {
            
            return photoArray.object(at: Int(index)) as! MWPhotoProtocol
            
        }
        
        return nil;
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
